// pages/clockRecords/clockRecords.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabNum:5,
    num:6,
    signRecords: [],
    months: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      nowDate:util.getNowDate(),
      scrollHeight:app.getScrollHeight(60)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getRecordList()
    this.getMonths()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  toDetail: function (e) {
    console.log('toDetail>>', e)
    let cardDate = e.currentTarget.dataset.date
    let uid = e.currentTarget.dataset.uid
    let type = e.currentTarget.dataset.type==1?1:2
    wx.navigateTo({
      url: '../clockDetail/clockDetail?cardDate=' + cardDate + '&uid=' + uid + '&type=' + type,
    })
  },  
  getRecordList:function(){
    let that = this
    let nowTime = new Date()
    let date = ''
    if( that.data.date ){
      date = that.data.date
    }else {
      date = nowTime.getFullYear() + '-' + (nowTime.getMonth() + 1)
    }
    lbbService.getMonthCard({ date: date }, function(res){
      that.setData({
        recordList: res.data.reverse()
      })
    })
  },
  toggleTab: function (e) {
    console.log('>>', e)
    let that = this
    let index = e.currentTarget.dataset.index
    that.setData({
      tabNum: index,
      date: that.data.months[index].date
    })
    that.getRecordList()
  },
  getMonths: function(){
    let that = this
    let months = []
    let date = new Date()
    let num = this.data.num
    var year1 = date.getFullYear()
    let month1 = date.getMonth() + 1
    for ( let i = 0; i < num; i++ ) {
      var month = month1 - i
      if (month < 1){
        var year = year1 - 1
        month += 12
      } else {
        var year = year1
      }
      var item = {}
      item.date = year + '-' + month
      item.month = month + '月'
      months.push(item)
    }
    this.setData({
      months: months.reverse()
    })
  }
})